/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./vResourcesManager.h"
#include "../../Macros/vLogMacros.h"

namespace Vendetta {
	vTexture2D* vResourcesManager::Texture2D(const vString& texName) {
		if (!mTextures2D.Contains(texName)) {
			return vNull;
		}

		return mTextures2D[texName];
	}

	vBool vResourcesManager::LoadResources(vResourcesLoader& loader) {
		createResources(loader);

		return true;
	}

	vResourcesManager::vResourcesManager() {
	}

	vResourcesManager::~vResourcesManager() {
		deleteTextures();
	}


	vResourcesManager::vResourcesManager(const vResourcesManager& factory) {

	}

	vResourcesManager& vResourcesManager::operator=(const vResourcesManager& factory) {

		return *this;
	}

	vVoid vResourcesManager::createResources(vResourcesLoader& loader) {
		LOGS_INFO("Creating resources...");
		createTextures(loader);
		LOGS_INFO("Resources created successfully.");
	}

	vVoid vResourcesManager::createTextures(vResourcesLoader& loader) {
		vTexture2D* tex = vNull;
		vResourcesLoader::ResList list = loader.ResourceGroup("Textures");
		
		if (!list.IsEmpty()) {
			LOGS_INFO("  Creating textures...");
			for (vUInt uiPos = 0; uiPos < list.Size(); uiPos++) {
				try {
					tex = new vTexture2D(list[uiPos].mFileName);
					mTextures2D<<vPair<vString, vTexture2D*>(list[uiPos].mResourceName, tex);
					LOGS_INFO("    Texture2D \"%s\" file \"%s\" [OK]", 
						list[uiPos].mResourceName.ToConstCharPtr(),list[uiPos].mFileName.ToConstCharPtr());
				} catch (vFatalException& e) {
					LOGS_INFO("    Texture2D \"%s\" file \"%s\" [FAILED]", 
						list[uiPos].mResourceName.ToConstCharPtr(),list[uiPos].mFileName.ToConstCharPtr());
					throw e;
				}
			}
		}
	}

	vVoid vResourcesManager::deleteTextures() {
		for (vUInt uiPos = 0; uiPos < mTextures2D.Size(); uiPos++) {
			vPair<vString, vTexture2D*> pair = mTextures2D.Element(uiPos);
			vDelete(pair.Second);
		}
		mTextures2D.Clear();
	}
}
